<template>
  <c-view>
    <template v-slot:header>
      <div class="title">
        {{ $route.meta.title }}
      </div>
    </template>
    <div class="main__box">
      <c-table
        ref="cTable"
        selection
        hasBorder
        :max-height="maxHeight"
        :size="size"
        :loading="isLoading"
        :table-header="tableHeader"
        :table-list="tableList"
        :table-inner-btns="tableInnerBtns"
        :page-info="pageInfo"
        @change-pagination="changePagination"
      >
        <template v-slot:header>
          <c-search :form-model="searchObj" :form-items="searchItems" @submit-form="searchSubmit" @reset-form="searchReset"></c-search>
        </template>
      </c-table>
    </div>
    <div v-if="dialogObj.isShow">
      <c-dialog
        class="dialog-box"
        :is-show="dialogObj.isShow"
        :title="dialogObj.title"
        close-btn
        @before-close="dialogObj.isShow = false"
        @on-submit="dialogConfirm"
      >
        <el-form ref="formRef" :model="formModel" :rules="rules" label-width="120px" class="form" label-position="right" status-icon>
          <el-form-item label="参数值" prop="value">
            <el-input placeholder="请输入参数值" v-model.trim="formModel.value"></el-input>
          </el-form-item>
          <el-form-item label="冻结原因" prop="blockingReason">
            <el-input
              type="textarea"
              :rows="4"
              maxlength="100"
              show-word-limit
              placeholder="请输入冻结原因"
              v-model.trim="formModel.remark"
            ></el-input>
          </el-form-item>
        </el-form>
      </c-dialog>
    </div>
  </c-view>
</template>

<script>
import mixinTable from 'mixins/table'
import CDialog from 'components/dialog'

export default {
  name: 'systemList',
  mixins: [mixinTable],
  components: {
    CDialog
  },
  data() {
    return {
      dialogObj: {},
      formModel: {
        value: '',
        remark: ''
      },
      rules: {
        value: [{ required: true, message: '参数值不能为空', trigger: 'blur' }]
      },
      tableInnerBtns: [
        // {
        //   name: '修改',
        //   icon: 'el-icon-edit',
        //   handle(row) {
        //     vm.showDialog({
        //       title: '参数修改',
        //       initData: {
        //         id: row.id,
        //         value: row.value,
        //         remark: row.remark
        //       }
        //     })
        //   }
        // }
      ],
      // 表格内操作按钮
      tableHeader: [
        {
          label: '参数名',
          prop: 'key'
        },
        {
          label: '参数值',
          prop: 'value'
        },
        {
          label: '参数单位',
          prop: 'unit'
        },
        {
          label: '参数类型',
          prop: 'type'
        },
        {
          label: '描述',
          prop: 'remark'
        }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    /*
     * 查询表格列表数据
     */
    fetchData() {
      const { pageNo, pageSize } = this.pageInfo
      const { dateTime, ...other } = this.searchObj
      const searchDate = this.getSearchDate(dateTime, 'dateTime')
      this.isLoading = true
      this.$api.settings
        .fetch('sysParamList', {
          ...searchDate,
          ...other,
          rows: pageNo,
          offset: pageSize
        })
        .then(res => {
          this.isLoading = false
          if (res && res.totalCount) {
            const { data, totalCount } = res
            this.pageInfo.totalNum = totalCount
            this.tableList = data || []
          } else {
            this.tableList = res || []
          }
        })
    },
    //解冻、冻结账户
    operateMoney(options) {
      console.log(options.moneyStatus)
    },
    /**
     * 对话框确认按钮，集成了表单提交功能
     */
    dialogConfirm() {
      const childRef = this.$refs.formRef
      childRef.validate(valid => {
        if (valid) {
          const { value, remark } = this.formModel
          const { id } = this.dialogObj.initData
          this.$api.settings
            .fetch('sysParamSaveOrUpdate', {
              value,
              remark,
              id
            })
            .then(() => {
              this.responeHandle('修改成功')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 显示对话框
    showDialog(opts) {
      this.dialogObj = {
        isShow: true,
        title: opts.title,
        initData: opts.initData
      }
    }
  }
}
</script>
